import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ActivatedRoute } from '@angular/router'
import { ModalService } from '@app/shared/utils'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'

@Component({
  selector: 'app-customer-assign',
  templateUrl: './customer-assign.component.html',
  styleUrls: ['./customer-assign.component.css'],
  providers: [ApiService]
})
export class CustomerAssignComponent implements OnInit {
  public assigndata = []
  public editRrecordno

  @ViewChild('editTemplate', { static: true })
  private editTemplateTemp: TemplateRef<any>

  formGroup:FormGroup

  constructor(
    private fb: FormBuilder,
    private apiService: ApiService,
    public modal: ModalService,
    public dictService: DictService,
    public cursorService: CursorService,
    private message: QlMessageService,
    public pageService: PageService,
    private router: Router,
    private store: Store,
    public route: ActivatedRoute
  ) {
    this.formGroup = fb.group({
      orgname:[null],
      managername: [null],
      managerroleno:[null],
    })
  }

  ngOnInit() {
    this.queryList()
  }

  public restFun(){
    this.formGroup.reset()
  }

  public queryList() {
    this.apiService.getcuctomerAssignList(this.cursorService).subscribe(data => {
      this.assigndata = data
    })
  }
  /**
   * 删除分配
   */
  public delAssign(ref: any) {
    this.apiService.deletecuctomerAssign(ref.rowData.recordno).subscribe(data => {
      this.message.success('删除成功')
      this.queryList()
    })
  }

  /**
   * 查询分配
   */
  editAssign(ref: any) {
    this.editRrecordno = ref.rowData.recordno
    this.modal
    .open({
      title: '编辑分配',
      component: this.editTemplateTemp
    })
    .subscribe(data => {
    })

    this.apiService.lookcuctomerAssign(ref.rowData.recordno).subscribe(data => {
      this.formGroup.patchValue(data)
    })
  }

  /**
   * 编辑分配
   */
  public saveFun(){
    const param = {
      "orgName":this.formGroup.value.orgname,
      "managerName":this.formGroup.value.managername,
	    "managerRoleNo":this.formGroup.value.managerroleno,
    }
    this.apiService.editcuctomerAssign(param,this.editRrecordno).subscribe(data => {
      // this.formGroup.patchValue(data)
      this.queryList()
      this.modal.close()
    })
  }
}
